package com.tom.demo.link;

import java.util.HashSet;
import java.util.Set;

public class Test141_CycleLink {
    static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }


    public boolean hasCycle(ListNode head) {
        Set<Integer> visited = new HashSet<>();
        while (head != null) {
            if (!visited.add(head.hashCode())) {
                return true;
            }
            head = head.next;
        }
        return false;
    }
}
